package com.julian.warehouse.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.julian.warehouse.R;
import com.julian.warehouse.bean.GoodsBean;
import com.julian.warehouse.interfaces.OnAddReduceClickListener;
import com.julian.warehouse.utils.MyLog;
import com.julian.warehouse.view.AmountView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/8/9.
 */

public class RukuLvAdapter extends BaseAdapter {
    private Context mContext;
    private List<GoodsBean> dataList;
    private List<Map<String, Integer>> countList;
    private OnAddReduceClickListener onAddReduceClickListener;


    public RukuLvAdapter(Context mContext, List<GoodsBean> dataList){
        this.mContext = mContext;
        this.dataList = dataList;
        countList = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_lv_ruku,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        final GoodsBean goodsBean = dataList.get(position);
        Log.i("AA","adapter--数据--goodsBean="+dataList.toString());
        holder.tv_gname.setText(goodsBean.getGname());
        holder.tv_kucun.setText("库存："+goodsBean.getCount()+"");
        holder.tv_price.setText("售价："+goodsBean.getPrice()+"");


        holder.mAmountView.setGoods_storage(99999);
        holder.mAmountView.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
            @Override
            public void onAmountChange(View view, int amount) {
                MyLog.i("AA","amount="+amount);

                    recordCount(holder.tv_gname.getText().toString().trim(),amount);


            }
        });



        return convertView;
    }


    class ViewHolder{
        private final TextView tv_gname;
        private final TextView tv_kucun;
        private final TextView tv_price;
        private ImageView iv_goods;
        private AmountView mAmountView;


        public ViewHolder(View v){

            iv_goods = v.findViewById(R.id.iv_goods);
            tv_gname = v.findViewById(R.id.tv_gname);
            tv_kucun = v.findViewById(R.id.tv_kucun);
            tv_price = v.findViewById(R.id.tv_price);
            mAmountView = (AmountView) v.findViewById(R.id.amount_view);
        }

    }

    private void recordCount(String gname,int count){

        Map<String, Integer> map = new HashMap<>();
        map.put(gname,count);

        for (int i = 0; i < countList.size(); i++) {
            if (countList.get(i).containsKey(gname)){
                countList.remove(i);
                Log.i("AA","获得的map中gname = "+gname+"重复已被移除，list.size="+countList.size());
            }
        }

        countList.add(map);
        if (onAddReduceClickListener!=null)
        onAddReduceClickListener.onAddReduceClick(countList);
        Log.i("AA","adapter中最终得到的countlist内容"+countList.toString());
    }

    public void setOnAddReduceClickListener(OnAddReduceClickListener listener){
        this.onAddReduceClickListener = listener;

    }


}
